<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 单页管理
 */
class ProjectController extends BaseController
{
    /**
     * 单页列表
     * @return [type] [description]
     */
    public function index($key="")
    {
        if($key === ""){
            $model = M('project');  
        }else{
            // $where['title'] = array('like',"%$key%");
            $where['name'] = array('like',"%$key%");
            // $where['_logic'] = 'or';
            $model = M('project')->where($where); 
        } 
        
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $pages = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('model', $pages);
        $this->assign('page',$show);
        $this->display();     
    }

    /**
     * 添加单页
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $model = D("Project");
			
       //上传附件代码2017-3-15-----------------------------------------------------------
		   if(!empty($_FILES)){  //判断是否有文件上传
		   $config = array(
		   'rootPath'      =>  './Public/', //保存根路径
		   'savePath'      =>  'project/', //保存路径
		   );
			//print_r($_FILES); 打印上传图片路径
			$upload = new \Think\Upload($config); //有图片上传实例化upload，把文件上传指定目录
            $cg = $upload -> upload(array($_FILES['pic']));//调用upload函数并将参数$_FILES数组传进去
			if(!$cg){
			show_bug($upload -> getError());//获得上传附件产生的错误信息
			}
			else{
			//循环输出拼装好的图片路径给$bigimg
			foreach($cg as $vo)
			{
			$bigimg.=$vo['savepath'].$vo['savename']."|";
			$_POST['pimgurl_1'] = $bigimg;
			}
			}

            //-------------------------------------------
            $cg1 = $upload -> upload(array($_FILES['pic1']));
			if(!$cg1){
			show_bug($upload -> getError());
			}
			else{
			foreach($cg1 as $vo1)
			{
			$bigimg1.=$vo1['savepath'].$vo1['savename']."|";
			$_POST['pimgurl_2'] = $bigimg1;
			}
			}

            //-------------------------------------------
            $cg2 = $upload -> upload(array($_FILES['pic2']));
			if(!$cg2){
			show_bug($upload -> getError());
			}
			else{
			foreach($cg2 as $vo2)
			{
			$bigimg2.=$vo2['savepath'].$vo2['savename']."|";
			$_POST['pimgurl_3'] = $bigimg2;
			}
			}

            //-------------------------------------------
            $cg3 = $upload -> upload(array($_FILES['pic3']));
			if(!$cg3){
			show_bug($upload -> getError());
			}
			else{
			foreach($cg3 as $vo3)
			{
			$bigimg3.=$vo3['savepath'].$vo3['savename']."|";
			$_POST['pimgurl_4'] = $bigimg3;
			}
			}
			
			}	
			
			
			
			
			
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
                if ($id = $model->add()) {
                    $data["project_id"] = $id;
                    $style = $_POST["house_style"];
                    foreach ($style as $key => $value) {
                        $data["house_style"] = $value;
                        M()->table("project_house_style")->add($data);
                    }
                    unset($data["house_style"]);
                    $metro = $_POST["metro"];
                    foreach ($metro as $key => $value) {
                        $data["metro"] = $value;
                        M()->table("project_metro")->add($data);
                    }
                    unset($data["metro"]);
                    $price_region = $_POST["price"];
                    foreach ($price_region as $key => $value) {
                        $data["price_region"] = $value;
                        M()->table("project_price_region")->add($data);
                    }
                    $this->success("添加成功", U('project/index'));
                } else {
                    $this->error("添加失败");
                }
            }
        }
    }
    /**
     * 更新单页信息
     * @param  [type] $id [单页ID]
     * @return [type]     [description]
     */
    public function update($id)
    {
    		$id = intval($id);
        //默认显示添加表单
        if (!IS_POST) {
            $model = M('project')->where("id=%d",$id)->find();
            // $model["house_style"] = M("project_house_style")->where("project_id=".$id)->select();
            // $model["metro"] = M("project_metro")->where("project_id=".$id)->select();
            // $model["price_region"] = M("project_price_region")->where("project_id=".$id)->select();
            $this->assign('page',$model);
            $this->display();
        }
        if (IS_POST) {
            $model = D("Project");
			
			
        //上传附件代码2017-3-11-----------------------------------------------------------
		   if(!empty($_FILES)){  //判断是否有文件上传
		   $config = array(
		   'rootPath'      =>  './Public/', //保存根路径
		   'savePath'      =>  'project/', //保存路径
		   );
			//print_r($_FILES); 打印上传图片路径
			$upload = new \Think\Upload($config); //有图片上传实例化upload，把文件上传指定目录
            $cg = $upload -> upload(array($_FILES['pic']));//调用upload函数并将参数$_FILES数组传进去
			if(!$cg){
			//show_bug($upload -> getError());//获得上传附件产生的错误信息
			}
			else{
			//循环输出拼装好的图片路径给$bigimg
			foreach($cg as $vo)
			{
			$bigimg.=$vo['savepath'].$vo['savename']."|";
			}
			}

            //-------------------------------------------
            $cg1 = $upload -> upload(array($_FILES['pic1']));
			if(!$cg1){
			//show_bug($upload -> getError());
			}
			else{
			foreach($cg1 as $vo1)
			{
			$bigimg1.=$vo1['savepath'].$vo1['savename']."|";
			}
			}

            //-------------------------------------------
            $cg2 = $upload -> upload(array($_FILES['pic2']));
			if(!$cg2){
			//show_bug($upload -> getError());
			}
			else{
			foreach($cg2 as $vo2)
			{
			$bigimg2.=$vo2['savepath'].$vo2['savename']."|";
			}
			}

            //-------------------------------------------
            $cg3 = $upload -> upload(array($_FILES['pic3']));
			if(!$cg3){
			//show_bug($upload -> getError());
			}
			else{
			foreach($cg3 as $vo3)
			{
			$bigimg3.=$vo3['savepath'].$vo3['savename']."|";
			}
			}
			
			}	
			
			
			
            if (!$model->create()) {
                $this->error($model->getError());
            }else{
                $model->id=$id;
				$data = M('project')->field("pimgurl_1,pimgurl_2,pimgurl_3,pimgurl_4")->where("id=%d",$id)->find();
				//var_dump($data);
			    $model->pimgurl_1=$data['pimgurl_1'].$bigimg;
			    $model->pimgurl_2=$data['pimgurl_2'].$bigimg1;
			    $model->pimgurl_3=$data['pimgurl_3'].$bigimg2;
			    $model->pimgurl_4=$data['pimgurl_4'].$bigimg3;
		
				
                if ($model->save()) {
                    $this->success("更新成功", U('project/index'));
                } else {
                    $this->error("更新失败");
                }        
            }
        }
    }
    /**
     * 删除单页
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
    		$id = intval($id);
        $model = M('project');
        $result = $model->where("id=%d",$id)->delete();
        if($result){
            $this->success("删除成功", U('project/index'));
        }else{
            $this->error("删除失败");
        }
    }
	
	
	
         //删除数组指定值	
         public function delarr_1($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('project')->field("pimgurl_1")->where("id=%d",$id)->find();
             $datav=$data['pimgurl_1'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("project");
             $data['pimgurl_1'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo json_encode('数据更新成功！');
             }else{
             echo json_encode('数据更新失败！');
             }
             }
	
         //删除数组指定值	
         public function delarr_2($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('project')->field("pimgurl_2")->where("id=%d",$id)->find();
             $datav=$data['pimgurl_2'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("project");
             $data['pimgurl_2'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo json_encode('数据更新成功！');
             }else{
             echo json_encode('数据更新失败！');
             }
             }

         //删除数组指定值	
         public function delarr_3($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('project')->field("pimgurl_3")->where("id=%d",$id)->find();
             $datav=$data['pimgurl_3'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("project");
             $data['pimgurl_3'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo json_encode('数据更新成功！');
             }else{
             echo json_encode('数据更新失败！');
             }
             }

         //删除数组指定值	
         public function delarr_4($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('project')->field("pimgurl_4")->where("id=%d",$id)->find();
             $datav=$data['pimgurl_4'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("project");
             $data['pimgurl_4'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo json_encode('数据更新成功！');
             }else{
             echo json_encode('数据更新失败！');
             }
             }

}
